<div class="wrap">
	
	<?php $post_types = get_post_types( array('public' => true), 'names'); ?>
	
	<?php screen_icon(WP_Solr::SLUG); ?>
	<h2><?php _e('Tools', WP_Solr::TEXT_DOMAIN); ?></h2>
	
	<div id="wp-solr-result"></div>
	
	<div id="wp-solr-loading" class="wp-solr-ui-theme-a wp-solr-ui-rounded-all" style="display:none;">
		<p><?php _e('Processing your request', WP_Solr::TEXT_DOMAIN); ?></p>
		<img src="<?php echo WP_Solr::$plugin_url; ?>/images/ajax-loader.gif" width="220px" height="19px" />
	</div>
	
	<div class="clear"></div>
	
	<h3><?php _e('Status', WP_Solr::TEXT_DOMAIN); ?></h3>
	
	
	<div class="form-table wp-solr wp-solr-ui-rounded-all">
		<div id="chart"></div>
		<table class="chart-table chart-status">
			<tbody>
				<tr class="odd" valign="top">
					<th scope="row"><?php _e('Ping: ', WP_Solr::TEXT_DOMAIN); ?></th>
					<td>
						<?php $ping = WP_Solr_Service::ping(); ?>
						<?php if ( !empty($ping) ) { ?>
							<code><?php echo round($ping, 3); ?> <?php _e(' seconds', WP_Solr::TEXT_DOMAIN); ?></code>
						<?php } else { ?>
							<code class="wp-solr-error"><?php _e('There seems to be a problem with your Solr', WP_Solr::TEXT_DOMAIN); ?></code>
						<?php } ?>
					</td>
				</tr>
				
				<?php $js_data = array(); ?>
				<?php foreach ( $post_types as $post_type ) { ?>
					<?php if ( $post_type !== 'attachment') { ?>
						<?php $css_class = ( $css_class == 'even' ) ? 'odd' : 'even'; ?>
						<tr class="<?php echo $css_class?>" valign="top">
							<th scope="row"><?php echo ucfirst($post_type); ?></th>
							<td>
							<?php $result = 0; ?>
							<?php $response = WP_Solr_Service::search('type:' . $post_type, 0, 0); ?>
							<?php if ( !empty($response) ) { ?>
								<?php $result = (int) $response->response->numFound; ?>
								<code><?php echo $result; ?></code>
							<?php } else { ?> 
								<code class="wp-solr-error"><?php echo $result; ?></code>
							<?php } ?>
							<?php $js_data[] = array('type' => ucfirst($post_type), 'result' => $result ); ?>
							</td>
						</tr> 
					<?php } ?>
				<?php } ?>
				
			</tbody>
		</table>
	</div>
	
	<?php
		$data_sets = '';
		$ticks = '';
		$i = 0;
		$count = count($js_data);
		foreach ( $js_data as $data ) {
			$end = ( $i + 1 == $count ) ? "" : ",";
			$data_sets .= sprintf(' [%s,%s]%s ', $i, $data['result'], $end);
			$ticks .= sprintf(' [%s,"%s"]%s ', $i, $data['type'], $end);
			$i++;			
		}
	?> 
	
	<script type="text/javascript">
	(function($) {
    
		$(function() {
			
			var datasets = [
				{ data: [<?php echo $data_sets; ?>] }
			];
			
			var options = {
					series: { lines: { show: true, fill: 0.15, lineWidth: 2 }, 
									  points: { show: true },
									  shadowSize: 3 },
					colors: [ "#21759B" ],
					grid:   { show: true, color: "#333",  backgroundColor: "#fff", borderColor: "#999",
									  tickColor: "#ccc", aboveData: false, borderWidth: 2, hoverable: true,
									  autoHighlight: true, mouseActiveRadius: 30 }, 
					legend: { labelBoxBorderColor: null, position: "nw", margin: 10, backgroundColor: null,  noColumns:1
									},                              
					xaxis:  { tickDecimals: 0, ticks: [<?php echo $ticks; ?>] },
					yaxis: { min: 0 }
			};
			$.plot($("#chart"), datasets, options);
		});
	
	})(jQuery);
	</script>
	
	
	<h3><?php _e('WP Import and Remove', WP_Solr::TEXT_DOMAIN); ?></h3>
	<form id="wp-solr-action-form" class="wp-solr" action="admin-ajax.php" method="post">
		
		<div class="form-table">
			<fieldset>
				<select id="wp-solr-action" name="wp_solr_action">
					<option value=""><?php _e('Choose an option... ', WP_Solr::TEXT_DOMAIN); ?></option>
					<option value="load"><?php _e('Load Types', WP_Solr::TEXT_DOMAIN); ?></option>
					<option value="delete"><?php _e('Delete Types', WP_Solr::TEXT_DOMAIN); ?></option>
					<option value="flush"><?php _e('Delete All', WP_Solr::TEXT_DOMAIN); ?></option>
				</select>
			</fieldset>	
			
			<fieldset id="wp-solr-load-types" style="display:none;">
				<?php foreach ( $post_types as $post_type ) { ?>
					<?php if ( $post_type !== 'attachment') { ?>
						<label>
							<input name="wp_solr_type" type="radio" value="<?php echo $post_type; ?>" /> <?php echo ucfirst($post_type); ?>
						</label>
					<?php } ?>
				<?php } ?>
			</fieldset>
			
			<fieldset id="wp-solr-delete-types" style="display:none;">
				<?php foreach ( $post_types as $post_type ) { ?>
					<?php if ( $post_type !== 'attachment') { ?>
						<label>
							<input name="wp_solr_delete_type" type="radio" value="<?php echo $post_type; ?>" /> <?php echo ucfirst($post_type); ?>
						</label>
					<?php } ?>
				<?php } ?>
			</fieldset>
		
		</div>
		
		<?php submit_button(__('Do action', WP_Solr::TEXT_DOMAIN)); ?>
	
		
	</form>
	
</div>